package com.huazi.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/*
封装回话的工具类
 */
public class SessionUtil {
//    回话工厂
  private static SqlSessionFactory factory=null;
    static {
        try {
            factory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    获取回话
    public static SqlSession getSession(){
        SqlSession sqlSession=null;
        if(factory!=null) {
            sqlSession = factory.openSession();
        }
        return sqlSession;
    }
}
